package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author zcf
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;


    @Resource
    private DishMapper dishMapper;

    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //1.查询购物车该商品是否已存在
        Long user_id = BaseContext.getCurrentId();
        shoppingCart.setUserId(user_id);

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (!CollectionUtils.isEmpty(list)) {
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shoppingCartMapper.update(shoppingCart1);
        } else {
            //为空
            //2.添加,判断是添加菜品还是套餐
            if (shoppingCartDTO.getDishId() != null) {
                //存菜品
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
                shoppingCartMapper.insert(shoppingCart);
            } else {
                //存套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCartMapper.insert(shoppingCart);
            }

        }
    }

    /**
     * 购物车列表
     *
     * @return
     */

    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.list2();
    }


    /**
     * 清空购物车
     */

    @Override
    public void clean() {
        Long id = BaseContext.getCurrentId();
        shoppingCartMapper.clean(id);
    }


    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(!CollectionUtils.isEmpty(list)){
          list.forEach(
                  (item)->{
                      if(item.getNumber()==1){
                          shoppingCartMapper.delete(item);
                      }else{
                          item.setNumber(item.getNumber()-1);
                          shoppingCartMapper.update(item);
                      }
                  }
          );
        }
    }
}
